package dj.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dj.Constant;
import dj.dao.BaseDao;
import dj.model.Menu;
import dj.pageModel.Pmenu;
import dj.pageModel.Puser;
import dj.service.MenuService;

@Service("menuService")
public class MenuServiceImpl implements MenuService {

	private BaseDao<Menu> menuDao;

	public BaseDao<Menu> getMenuDao() {
		return menuDao;
	}

	@Autowired
	public void setMenuDao(BaseDao<Menu> menuDao) {
		this.menuDao = menuDao;
	}

	public List<Pmenu> getTopMenu(Puser user) {
		List<Pmenu> pmenus = new ArrayList<Pmenu>();

		String hql = "from Menu m, MenuAuth a where a.userid=:userid and a.menu=m.id and m.menu is null order by m.seq";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userid", user.getId());
		List<Menu> menus = menuDao.find(hql, params);
		if (null != menus && menus.size() > 0) {
			for (Menu menu : menus) {
				setMenu(pmenus, menu, menus);
			}
		}
		return pmenus;
	}

	public List<Pmenu> getMenuByUser(Puser user) {
		List<Pmenu> pmenus = new ArrayList<Pmenu>();

		List<Menu> menus = new ArrayList<Menu>();
		Map<String, Menu> temp;
		String hql = "";
		if ("admin".equals(user.getName())) {
			hql = "from Menu order by seq";
			menus = menuDao.find(hql);
		} else {
			hql = "select m from Menu m, MenuAuth a where a.memberid=:memberid and a.menu=m.id and a.type=:user order by m.seq";
			String sysRoleHql = "select m from Menu m, MenuAuth a, SysRoleMembers srm, SysRoles sr where srm.memberid=:memberid and srm.sysRoles=sr.id and sr.id=a.memberid and a.type=:role and a.menu=m.id order by m.seq";
			Map<String, Object> params = new HashMap<String, Object>();
			Map<String, Object> roleParams = new HashMap<String, Object>();
			params.put("memberid", user.getId());
			params.put("user", Constant.MENU_MEMBER_TYPE_USER);
			roleParams.put("memberid", user.getId());
			roleParams.put("role", Constant.MENU_MEMBER_TYPE_ROLE);
			menus = menuDao.find(hql, params);
			menus.addAll(menuDao.find(sysRoleHql, roleParams));
		}

		if (null != menus && menus.size() > 0) {
			temp = new LinkedHashMap<String, Menu>();
			// 去下重复，系统角色菜单可能和人员菜单重复
			for (Menu menu : menus) {
				temp.put(menu.getId(), menu);
			}
			menus = new ArrayList<Menu>(temp.values());
			for (Menu menu : menus) {
				setMenu(pmenus, menu, menus);
			}
		}

		return pmenus;
	}

	public void setMenu(List<Pmenu> pmenus, Menu menu, List<Menu> menus) {
		Pmenu pmenu = new Pmenu();
		BeanUtils.copyProperties(menu, pmenu);
		pmenu.setText(menu.getName());
		Map<String, Object> attributes = new HashMap<String, Object>();
		attributes.put("url", menu.getUrl());
		pmenu.setAttributes(attributes);
		Menu m = menu.getMenu(); // 获得当前节点的父节点对象
		if (m != null) {
			pmenu.setPid(m.getId());
			//菜单列表中没有其父菜单，则添加进去
			boolean isAddPmenu = true;
			if(!menus.contains(m)) {
				for(Pmenu p : pmenus) {
					if(p.getId().equals(m.getId())) {
						isAddPmenu = false;
					}
				}
				if(isAddPmenu) {
					setMenu(pmenus, m, menus);
				}
			}
		}
		pmenus.add(pmenu);
	}

	public List<Pmenu> getMenuChildren(Pmenu menu, List<Pmenu> menus) {
		List<Pmenu> pmenus = new ArrayList<Pmenu>();
		for(Pmenu pmenu : menus) {
			if(menu.getId().equals(pmenu.getPid())) {
				pmenus.add(pmenu);
				getMenuChildren(pmenu, menus);
			}
		}
		return pmenus;
	}

}
